package org.example.Conrtoller;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelGenerator {
    public static void main(String[] args) {
        // 创建表头
        String[] headers = {"ID", "Name", "Age", "City", "Address"};

        // 生成数据
        Object[][] data = new Object[200][headers.length];
        for (int i = 0; i < 200; i++) {
            data[i][0] = i + 1;
            data[i][1] = ChineseUtil.randomChineseName();
            data[i][2] = i + 20;
            data[i][3] = "City_" + (i + 1);
            data[i][4] = "Address_" + (i + 1);
        }

        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        // 写入表头
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 写入数据
        for (int i = 0; i < data.length; i++) {
            Row row = sheet.createRow(i + 1);
            for (int j = 0; j < data[i].length; j++) {
                Cell cell = row.createCell(j);
                //a[i][j]表示,数组元素的i行j列
                cell.setCellValue(data[i][j].toString());
            }
        }

        // 保存 Excel 文件到 C 盘根目录
        try (FileOutputStream outputStream = new FileOutputStream("D:\\Test.xlsx")) {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}